var DC = new Class({
    Implements : [Events, Options],
    options: {
        form_marker: 'kool-form'
    }, 
    initialize: function(){
        this.init();
        this.initImage();
        this.siteMenu('pagemenu_side', 'div.toggler', 'div.element');   
        this.addEvent('init', function(){
           this.init();
           this.initImage(); 
        });
    },
    init: function(){
        var rh = this; 
        $$('form.'+rh.options.form_marker).each(function(form_obj)
        {
            rh.addFieldsListeners(form_obj);
            if(!form_obj.retrieve('rh'))
            {   var area = form_obj.get('rel');
                form_obj.store('rh', true);
                if(form_obj.retrieve('submitbtn'))
                {
                    form_obj.store('submitbtn', '');
                }
                form_obj.addEvent('submit', function(event){
                    event.stop();
                    if(rh.verifyForm(form_obj))
                    {
                    alert(area);
                        rh.onSubmit(form_obj,area);
                    }
                }.bind(this));
            }
        });
        $$('input.DatePicker').each( function(el){
            new DatePicker(el);
        });
/*    
        $$('a').each(function(link_obj){
             if(link_obj.retrieve('rh') != true){         
                var caller = link_obj.get('rev');
                var area = link_obj.get('rel');
                var link = link_obj.get('href');
                if(caller=='pagelink' && area !=null){
                    link_obj.store('rh', true);   
                    link_obj.addEvent('click', function(e) {
                        e = new Event(e).stop();
                        rh.handleCaller(link,area);
                     }.bind(this)); 
                 }
                
                if(caller!=null){
                    link_obj.store('rh', true);   
                    link_obj.addEvent('click', function(e) {
                        e = new Event(e).stop();
                        rh.handleCaller(link,area);
                    }.bind(this));  
                }
            }
        });     */
    }, 
    
    _is_ipt_select: function(iptObj)
    {
        if(iptObj.value==0 || iptObj.value=='' || !iptObj.value || iptObj.value == null)
        {
            return true;
        }
        return false;
    },                        
    _is_box_check: function(cssName)
    {
     var ret= false;
        $$('input.'+cssName+'[type=checkbox]').each(function(checkbox){
            if(checkbox.checked){
                ret = true;
            }
        });
        return ret;
    },
    verify: function(iptEl, errId)
    {
        var error = ['',
        '_ajax_check', 
        '_is_ipt_blank',
        '_is_ipt_short',
        '_is_ipt_select'
        ];
        if(!error[errId]) return false;
        if(this[error[errId]](iptEl))
        {
            //return this._get_error_msg(errId, iptEl);
        }
        return false;
    },
     verifyForm: function(this_form)
    {
        var i= 0;
        var this_request_handler = this;
        if(this.errArray) this.errArray.empty();
        this.errArray = [];
        var flag = false;
        if(this.errBox) this.errBox.dispose();
        else this.errBox = new Element ( "div", { 'id' : 'error-message' });
        this.errBox.set( 'text', "Lỗi trả về" );
        var field = this_form.getElements('input').extend(this_form.getElements('select')).extend(this_form.getElements('textarea'));
        field.each( function(el)
        {
            var errId = el.get('rel');
            if(errId)
            {
                var errMsg = this_request_handler.verify(el, errId);
                if(errMsg)
                {
                    this_request_handler.errArray.include( ' - ' + errMsg);
                    this_request_handler._alert_error_ipt(el, errMsg);
                }                
            }
            if(el.hasClass('search-input')){
                var submitbtn = this_form.retrieve('submitbtn');  
                if(submitbtn=='cmdSearch' || submitbtn == null)
                {
                    var length = el.value.length; 
                    if(length<3){
                        this_request_handler.alert.generateAlert('Độ dài tìm kiếm ít nhất là 3 ký tự',{ 'type': 'errorbx', 'txtTitle':'.:Có lỗi (DCxC0009)'});
                        flag = true;
                    }
                }
            }
        });
        if(this.errArray.length)
        {
            this.errArray.each( function(msg){
                this_request_handler.errBox.adopt(
                    new Element( "div", { 'text': msg })
                );
            });
            this.errBox.inject(this_form, 'before');
            window.scrollTo(this.errBox.offsetLeft, this.errBox.offsetTop);
            return false;
        }
        return !flag;        
    },
    toggleCB: function(cb_obj)
    {
        var cb_css = cb_obj.get('rev');
        if(!cb_css) return;
        $$('input.'+cb_css+'[type=checkbox]').each(function(checkbox){
            checkbox.checked = cb_obj.checked;
        });
    },
    onSubmit: function(this_form, area)
    {
        var rh = this;
        if (!this_form.getElement('input[name=sendby]')){
            this_form.adopt( new Element("input", { 'type': 'hidden', 'name': 'sendby','value': this.options.form_marker} ));
        }
        var submitbtn = this_form.retrieve('submitbtn');
        if(submitbtn)
        {
            this_form.adopt(new Element("input", { 'type': 'hidden', 'name' : 'submitbtn', 'value': submitbtn}));
        }
        var rh = this;
        var this_request = new Request({
            method: 'post',
            url: this_form.get('action'),
            autoCancel: true,
            data: this_form,
            async: false,
            onSuccess : function(data){
                $(area).innerHTML = data; 
                $('loading').setProperty('class','nomal') 
            },
            onRequest: function(){
                $('loading').setProperty('class','loading')
            },
            onFailure: function(){alert('Co loi roi!');},
            onException: function() {alert('Co bug!');}
        }).send();
        rh.fireDomReady();
    },
    //Call function control link
    handleCaller: function(url,area){
        var rh = this; 
        var request = new Request({
            method: 'get',
            url: url,
            async: false,
            autoCancel: true,
            onSuccess : function(data){
                $(area).innerHTML = data; 
                $('loading').setProperty('class','nomal') 
            },
            onRequest: function(){
                $('loading').setProperty('class','loading')
            }
        }).send();  
        rh.fireDomReady();  
    },
    initImage:function(){
        $$('img.gallery-item').each(function(el){
            el.addEvent('mouseenter', function(){ this.fade(0.5) });
            el.addEvent('mouseout', function(){
                this.fade(1); 
            });
        })
    },     
    /* Tips 2 */
    initTips:function(){
        var tips_friend = new Tips(('input.sigle'), {
            offsets: {x: 0, y: -30}, 
            className: 'tip-style',
            fixed: true
        })     
    },
    siteMenu: function(accordion,toggler,element){
        var display = $(accordion).get('rel');
        var myAccordion = new Accordion($(accordion), toggler, element, {
            opacity: true,
            show: false,
            display: display,
            alwaysHide: true
        });
    },
    fireDomReady: function()
    {
       //console.log('Oke'); 
       //alert('oke'); 
       this.fireEvent('init'); 
       //console.log('Oke 1');   
    }
    
});  

